1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect.testing.testers;
18
19 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21
22 import com.google.common.annotations.GwtCompatible;
23 import com.google.common.collect.testing.WrongType;
24 import com.google.common.collect.testing.features.CollectionFeature;
25 import com.google.common.collect.testing.features.CollectionSize;
26
27
28
29
30
31
32
33 @GwtCompatible
34 public abstract class AbstractListIndexOfTester<E>
35 extends AbstractListTester<E> {
36
37 protected abstract int find(Object o);
38
39
40
41
42
43 protected abstract String getMethodName();
44
45 @CollectionSize.Require(absent = ZERO)
46 public void testFind_yes() {
47 assertEquals(getMethodName() + "(firstElement) should return 0",
48 0, find(getOrderedElements().get(0)));
49 }
50
51 public void testFind_no() {
52 assertEquals(getMethodName() + "(notPresent) should return -1",
53 -1, find(samples.e3));
54 }
55
56 @CollectionFeature.Require(ALLOWS_NULL_VALUES)
57 public void testFind_nullNotContainedButSupported() {
58 assertEquals(getMethodName() + "(nullNotPresent) should return -1",
59 -1, find(null));
60 }
61
62 @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
63 public void testFind_nullNotContainedAndUnsupported() {
64 try {
65 assertEquals(
66 getMethodName() + "(nullNotPresent) should return -1 or throw",
67 -1, find(null));
68 } catch (NullPointerException tolerated) {
69 }
70 }
71
72 @CollectionFeature.Require(ALLOWS_NULL_VALUES)
73 @CollectionSize.Require(absent = ZERO)
74 public void testFind_nonNullWhenNullContained() {
75 initCollectionWithNullElement();
76 assertEquals(getMethodName() + "(notPresent) should return -1",
77 -1, find(samples.e3));
78 }
79
80 @CollectionFeature.Require(ALLOWS_NULL_VALUES)
81 @CollectionSize.Require(absent = ZERO)
82 public void testFind_nullContained() {
83 initCollectionWithNullElement();
84 assertEquals(getMethodName() + "(null) should return " + getNullLocation(),
85 getNullLocation(), find(null));
86 }
87
88 public void testFind_wrongType() {
89 try {
90 assertEquals(getMethodName() + "(wrongType) should return -1 or throw",
91 -1, find(WrongType.VALUE));
92 } catch (ClassCastException tolerated) {
93 }
94 }
95 }